package apigoogle;

import org.scribe.model.*;
import org.scribe.oauth.OAuthService;

public class Google20Service implements OAuthService{
	private static final String VERSION = "2.0";
	private GoogleAPi api;
	private OAuthConfig config;


	
	public Google20Service(GoogleAPi api,OAuthConfig config) {
		this.config = config;
		this.api=api;
	}

	public Token getRequestToken() {
		throw new UnsupportedOperationException("Unsupported operation, please use 'getAuthorizationUrl' and redirect your users there");
	}

	public Token getAccessToken(Token token, Verifier verifier) {
		OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
		request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
		request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
		request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
		request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
		request.addBodyParameter("grant_type", "authorization_code"); // todo refactor to API with hook method

		if (config.hasScope()) request.addQuerystringParameter(OAuthConstants.SCOPE, config.getScope());
		Response response = request.send();
		return api.getAccessTokenExtractor().extract(response.getBody());
	}

	public void signRequest(Token accessToken, OAuthRequest request) {
		request.addQuerystringParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getToken());
	}

	public String getVersion() {
		return VERSION;
	}

	public String getAuthorizationUrl(Token token) {
		return api.getAuthorizationUrl(config);
	}

	public void addScope(String arg0) {
		// TODO Auto-generated method stub
		
	}

}
